package com.itheima.stock.service.impl;

import cn.hutool.core.date.DateTime;
import com.itheima.stock.constant.InsetConstant;
import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.pojo.vo.req.AddPermissionVo;
import com.itheima.stock.pojo.entity.SysPermission;
import com.itheima.stock.pojo.vo.req.EditPermissionVo;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.pojo.vo.resp.ResponseCode;
import com.itheima.stock.service.PermissionsService;
import com.itheima.stock.utils.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionsServiceImpl implements PermissionsService {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private IdWorker idWorker;

    @Override
    public R<List<SysPermission>> getPermissions() {
        List<SysPermission> sysPermissions = sysPermissionMapper.getPermissions();
        return R.ok(sysPermissions);
    }

    @Override
    public R<List<Map>> getPermissionsTree() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id","0");
        map.put("title","顶级菜单");
        map.put("level",0);
        List<Map> list = new ArrayList<>();
        List<Map> tree = sysPermissionMapper.getPermissionsTree();
        list.add(map);
        list.addAll(tree);
        return R.ok(list);
    }
    @Override
    public R addPermission(AddPermissionVo addPermissionVo) {
        List<Integer> types = sysPermissionMapper.getPTypeByPid(addPermissionVo.getPid());
        if(addPermissionVo.getType()==1){
            if(!types.contains(1)&&!(addPermissionVo.getPid().equals(0L))){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
            }
        }
        if(addPermissionVo.getType()==2){
            if(addPermissionVo.getName().isEmpty()){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_NAME_NULL);
            }
            if(!types.contains(1)){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_MENU_ERROR);
            }
        }

        if(addPermissionVo.getType()==3){
            if(addPermissionVo.getPerms().isEmpty()){
                return R.error(ResponseCode.OPERATION_BUTTON_PERMISSION_PERMS_NULL);
            }
            if(!types.contains(2)){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_BTN_ERROR);
            }
        }

        SysPermission permission = new SysPermission();
        BeanUtils.copyProperties(addPermissionVo,permission);
        permission.setDeleted(InsetConstant.INIT_DELETED);
        permission.setStatus(InsetConstant.INIT_STATUS);
        permission.setCreateTime(DateTime.now());
        permission.setUpdateTime(DateTime.now());
        permission.setId(idWorker.nextId());
        sysPermissionMapper.insertSelective(permission);
        return R.ok(ResponseCode.SUCCESS);
    }
    @Override
    public R editPermission(EditPermissionVo editPermissionVo) {
        List<Integer> types = sysPermissionMapper.getPTypeByPid(editPermissionVo.getPid());
        if(editPermissionVo.getType()==1){
            if(!types.contains(1)&&!(editPermissionVo.getPid().equals(0L))){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_CATALOG_ERROR);
            }
        }
        if(editPermissionVo.getType()==2){
            if(editPermissionVo.getName().isEmpty()){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_NAME_NULL);
            }
            if(!types.contains(1)){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_MENU_ERROR);
            }
        }

        if(editPermissionVo.getType()==3){
            if(editPermissionVo.getPerms().isEmpty()){
                return R.error(ResponseCode.OPERATION_BUTTON_PERMISSION_PERMS_NULL);
            }
            if(!types.contains(2)){
                return R.error(ResponseCode.OPERATION_MENU_PERMISSION_BTN_ERROR);
            }
        }
        SysPermission permission = new SysPermission();
        BeanUtils.copyProperties(editPermissionVo,permission);
        permission.setUpdateTime(DateTime.now());
        sysPermissionMapper.updateByPrimaryKeySelective(permission);
        return R.ok(ResponseCode.SUCCESS);
    }
    @Override
    public R delPermission(String permissionId) {
        List<Long> Pids = sysPermissionMapper.getAllPermissionPid();
        if(Pids.contains(Long.valueOf(permissionId))){
            return R.error(ResponseCode.ROLE_PERMISSION_RELATION);
        }
        sysPermissionMapper.deleteByPrimaryKey(Long.valueOf(permissionId));
        return R.ok(ResponseCode.SUCCESS);
    }
}
